//
    //  WBHomeViewController.swift
//      desunireWeibo
//
//  Created by desunire on 2017/8/24.
//  Copyright © 2017年 desunire. All rights reserved.
//

import UIKit

//全局常量
fileprivate let cellId = "cellID"

class WBHomeViewController: WBBaseViewController {

    
    //fileprivate lazy var statusList = [String]()
    
    fileprivate lazy var listViewModel = WBStatusListViewModel()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        loadData()
    }

    @objc fileprivate func showFriend(){
        print(#function)
        
        let vc = WBDemoViewController()
        
        navigationController?.pushViewController(vc, animated: true)
    }
    
    override func loadData() {
        print("最后一个数据 \(listViewModel.statusList.last?.text ?? "")")
        listViewModel.loadStatus(pullup: self.isPull){ (isSuccess) in
            self.isPull = false;
            self.tableView?.reloadData()
            self.refreshControl?.endRefreshing()
        }
        
        
//       WBNetworkManager.shared.statusList { (list, isSuccess) in
//            for dic in list{
//                let text = dic["text"] as! String
//                //print(">>\(dic)")
//                if self.isPull{
//                    self.statusList.append("上拉\(text)")
//                }else{
//                    self.statusList.insert(text, at: 0)
//                }
//                self.isPull = false;
//                self.tableView?.reloadData()
//                self.refreshControl?.endRefreshing()
//            }
//       }
        
//        let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
//        let params = ["access_token":"2.006MM_SD0Rbgzbe636678a79kdbE3C"]
        
//        WBNetworkManager.shared.get(urlString, parameters: params, progress: nil, success: { (_, json) in
//           // print(json!)
//            //let data = try?JSONSerialization.jsonObject(with: (json as? Data)!, options: [])
//            
//           // print(data ?? "")
//            
//            let data:[String:String] = json as! [String : String]
//            print(data)
//           // let _:[String:String] = data["statuses"] as [String : String]
//            
//            
////            if self.isPull{
////                self.statusList.append("上拉\(i)")
////            }else{
////                self.statusList.insert(i.description, at: 0)
////            }
//            print("end")
//            self.isPull = false;
//            self.tableView?.reloadData()
//            self.refreshControl?.endRefreshing()
//        }) { (_, error) in
//            print("error\(error)")
//        }
  
//        WBNetworkManager.shared.request(URLString: urlString, parameters: params as [String : AnyObject]) { (json,isSuccess) in
//            //print(json as AnyObject)
//           // print(json?["statuses"] as Any)
//            
////            let arr = json?["statuses"] as! [String:AnyObject]
////            for dic in arr{
////                            let text = dic["text"] as Any
////                            if self.isPull{
////                                self.statusList.append("上拉\(text)")
////                            }else{
////                                self.statusList.insert(text, at: 0)
////                            }
////                            print("end")
////                            self.isPull = false;
////                            self.tableView?.reloadData()
////                            self.refreshControl?.endRefreshing()
////            }
//            
//            guard let resultDic = json as? [String:AnyObject] else {
//                return
//            }
//            //print(resultDic)
//            guard let dataArr = resultDic["statuses"] as? [AnyObject] else {
//                return
//            }
//            
//            for dic in dataArr{
//                let text = dic["text"] as! String
//                //print(">>\(dic)")
//                    if self.isPull{
//                        self.statusList.append("上拉\(text)")
//                    }else{
//                        self.statusList.insert(text, at: 0)
//                    }
//                    print("end")
//                    self.isPull = false;
//                    self.tableView?.reloadData()
//                    self.refreshControl?.endRefreshing()
//            }
//            
//        }
//
//        WBNetworkManager.shared.request(URLString: urlString, parameters: params as [String : AnyObject]) { (json, isSuccess) in
//            
//        }
        //模拟延时加载数据
//        DispatchQueue.main.asyncAfter(deadline:DispatchTime.now()+1) {
//            for i in 0..<20 {
//                if self.isPull{
//                    self.statusList.append("上拉\(i)")
//                }else{
//                    self.statusList.insert(i.description, at: 0)
//                }
//                
//            }
//            print("end")
//            self.isPull = false;
//            self.tableView?.reloadData()
//            self.refreshControl?.endRefreshing()
//        }
        
    }
    
}

// MARK: - 表格数据源方法
extension WBHomeViewController{
    
    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return listViewModel.statusList.count
    }
    //重写
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath)
        
        cell.textLabel?.text = listViewModel.statusList[indexPath.row].text
        
        cell.textLabel?.numberOfLines = 0
        
        cell.selectionStyle = .none
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        return 100
    }
    
}


// MARK: -  设置界面
extension WBHomeViewController{
    
    
    override func setTableView() {
        
        super.setTableView()
        
        tableView?.register(UITableViewCell.self, forCellReuseIdentifier: cellId)
        
        let btn:UIButton = UIButton.cz_textButton("好友", fontSize: 16.0, normalColor: UIColor.darkGray, highlightedColor: UIColor.orange)
        
        btn.addTarget(self, action: #selector(showFriend), for:.touchUpInside)
        
        navItem.leftBarButtonItem = UIBarButtonItem(customView: btn)
    }
    
//    override func setUI(){
//
//        super.setUI()
//        tableView?.register(UITableViewCell.self, forCellReuseIdentifier: cellId)
//        
//        //navigationItem.leftBarButtonItem = UIBarButtonItem(title: "好友", style: .plain, target: self, action: #selector(showFriend))
//        
//        let btn:UIButton = UIButton.cz_textButton("好友", fontSize: 16.0, normalColor: UIColor.darkGray, highlightedColor: UIColor.orange)
//        
//        btn.addTarget(self, action: #selector(showFriend), for:.touchUpInside)
//        
//        navItem.leftBarButtonItem = UIBarButtonItem(customView: btn)
//        
//    }
    
}
